<?php
/**
 * 子应用入口
 * @param  array
 * @return [type]
 */
function run_comment_app($uri = array()) {
	if (isset($uri[2]) && !empty($uri[2])) {
		$action = $uri[2];
		$parameter = isset($uri[3]) ? trim($uri[3]) : '';
		$parameter = str_replace('.json', '', $parameter);
		switch ($action) {
		case 'list':
			require_once APP_PATH . '/comment/model.php';
			include_once "comment.json";
			//post_show_count($parameter);
			break;
		case 'post':
			require_once APP_PATH . '/page/index.php';
			//page_show_count($parameter);
			break;
		}
	}
}

/**
 * 统计并显示统计数据
 * @param  string
 * @return [type]
 */
function comment_show_count($hash = '') {
	if (empty($hash)) {
		return;
	}
	$hash = filter_data($hash);
	$ip = get_ip();
	$user_agent = $_SERVER['HTTP_USER_AGENT'];
	require_once APP_PATH . '/comment/model.php';
	$ret = comment_model_insert_count($hash, $ip, $user_agent);
	if (!$ret) {
		//同一个IP不允许重复记录
		//更新数据
		comment_model_update_count_by_hash($hash);
	}
	//获取数据
	$page_view_count = comment_model_get_count_by_hash($hash);

	//显示数据
	echo 'document.write("' . $page_view_count . '");';
}

/**
 * 显示comment页面
 * @param  string
 * @return [type]
 */
function comment_show_page($hash = '') {
	if (empty($hash)) {
		show_not_found();
		return;
	}
	$hash = filter_data($hash);
	// app start.
	if (is_file(POST_HTML_PATH . $hash . '.html') && (time() - filemtime(POST_HTML_PATH . $hash . '.html') < 1200) && !IS_DEBUG) {
		//缓存未失效则直接加载静态文件
		include POST_HTML_PATH . $hash . '.html';
	} else {
		require_once APP_PATH . '/comment/build.php';
		$comment_html_path = comment_build_page_by_hash($hash);
		//输出
		if ($comment_html_path) {
			include $comment_html_path;
		} else {
			show_not_found();
		}
	}
}